import wx
import wx.lib.newevent as ne
from word_panel import WordPanel
from control_panel import ControlPanel

# 自定义事件
UpdateWordEvent, EVT_UPDATE_WORD = ne.NewEvent()
StatusChangeEvent, EVT_STATUS_CHANGE = ne.NewEvent()


class WordLearnerFrame(wx.Frame):
    def __init__(self, config, data_loader):
        super().__init__(None, title="英语单词学习", size=(800, 600))
        self.config = config
        self.data_loader = data_loader
        self.current_page = self.config.get('current_page', 1)
        self.filter_status = self.config.get('filter_status', 'all')
        self.show_all_definitions = False
        self.config.set('show_all_definitions', False)

        self.init_ui()
        self.load_page(self.current_page)

        self.Bind(EVT_STATUS_CHANGE, self.on_status_change)

    def init_ui(self):
        # 主面板
        main_panel = wx.Panel(self)
        main_sizer = wx.BoxSizer(wx.VERTICAL)

        # 单词显示区域
        self.word_panel = WordPanel(main_panel, self.config)
        main_sizer.Add(self.word_panel, 1, wx.EXPAND | wx.ALL, 5)

        # 控制区域
        self.control_panel = ControlPanel(main_panel, self.config, self.data_loader)
        main_sizer.Add(self.control_panel, 0, wx.EXPAND | wx.ALL, 5)

        main_panel.SetSizer(main_sizer)

        # 菜单栏
        self.create_menu_bar()

        # 状态栏
        self.CreateStatusBar()
        self.SetStatusText("准备就绪")

        self.Centre()

    def create_menu_bar(self):
        menubar = wx.MenuBar()

        # 文件菜单
        file_menu = wx.Menu()
        exit_item = file_menu.Append(wx.ID_EXIT, "退出", "退出应用程序")
        self.Bind(wx.EVT_MENU, self.on_exit, exit_item)

        menubar.Append(file_menu, "文件")
        self.SetMenuBar(menubar)

    def load_page(self, page=1):
        self.current_page = page
        self.filter_status = self.config.get('filter_status', 'all')

        total_pages = self.data_loader.get_total_pages(
            filter_status=self.filter_status
        )

        if page < 1:
            page = 1
        elif page > total_pages:
            page = total_pages

        word_data = self.data_loader.get_page_data(
            page=page,
            filter_status=self.filter_status
        )

        self.word_panel.load_words(word_data, self.show_all_definitions)
        self.control_panel.update_page_info(page, total_pages)
        self.SetStatusText(f"已加载第 {page} 页，共 {total_pages} 页")

    def update_display_mode(self, show_all):
        self.show_all_definitions = show_all
        self.word_panel.update_display_mode(show_all)

    def on_status_change(self, event):
        idx = event.idx
        status = event.status
        word = self.word_panel.word_data[idx]['word']
        self.data_loader.update_word_status(word, status)
        self.SetStatusText(f"已更新单词 '{word}' 的学习状态")

    def on_exit(self, event):
        self.config.set('current_page', self.current_page)
        self.config.set('filter_status', self.filter_status)
        self.config.set('show_all_definitions', self.show_all_definitions)
        self.Close()